package web_jdbc;

import com.fasterxml.jackson.databind.ObjectMapper;
import web_jdbc.po.User;
import web_jdbc.service.IUserService;
import web_jdbc.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;



@WebServlet("/login")
public class Login extends HttpServlet {
    private ObjectMapper objectMapper = new ObjectMapper();
    IUserService iUserService ;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        iUserService = new UserService();
        User user = objectMapper.readValue(req.getInputStream(), User.class);
        System.out.println(user.toString());
        resp.setContentType("application/text;charset=utf8");//setContentType 这设置的是一个属性呀

        user = iUserService.select(user);
        if (user!=null) {
            // 用户存在，登录成功
            resp.getWriter().println("ok");
            System.out.println("登录成功");
        } else {
            // 用户不存在，登录失败
            System.out.println("用户名或密码错误");
            resp.getWriter().println("用户名或密码错误");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 处理GET请求
    }
}